import {ref,reactive} from '@vue/composition-api'
import { uploadImg } from "@/api/users";
import {Toast} from 'vant'
//use-Upload上传图片
export default function (file){
    const cropper= ref(null)
    const upload=reactive({
        avaterList:[],//头像
        documentsList:[],//证件
        degreeList:[], //学历
        lifeList:[],//生活
        isCrop:false,//是否要裁剪
    })
    //上传图片
    const   uploadFun=(file)=>{
        let params = new FormData();
        params.append("file", file);
       const{code,msg,data}= uploadImg(params)
        if(code===0){
            Toast(msg)
            return {
                code,data
            }
        }else{
            Toast(msg)
        }

    }
    //上传头像
    const onAvatar=(file)=>{
        const data =uploadFun(file)
        upload.avaterList=[data.url]
    }
    //上传证件
    const onLifeList=file=>{
        const data =uploadFun(file)
        upload.lifeList=[data.url]
    }
    //上传学历
    const onDegreeList=file=>{
        const data =uploadFun(file)
        upload.degreeList=[data.url]
    }
    //裁剪
    const getCropBlob=()=> {
       cropper.value.getCropBlob(data => {
           const typeArr = data.type.split('/')
            const files = new window.File([data], Date.parse(new Date()) + '.' + typeArr[1], { type: data.type })
           const params = new FormData();
            params.append("file", files);
           const{code,msg,data1}= uploadImg(params)
           if(code===0){
               Toast(msg)
               return {
                   code,data1
               }
           }else{
               Toast(msg)
           }
        })
    }
    return {
        onAvatar,
        onLifeList,
    onDegreeList
    }
}
